package pessoas
{
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.flixel.FlxG;
	import org.flixel.FlxPoint;
	import org.flixel.FlxState;
	import org.flixel.FlxSprite;
	import player.Salazar;
	
	/**
	 * ...
	 * @author Lucas Mello
	 */
	public class Midia extends PessoaCacadora
	{
		[Embed(source = '../../assets/mediaHead.png')] public static var head_img:Class;
		[Embed(source = '../../assets/midiaBody.png')] public static var body_img:Class;
		[Embed(source = '../../assets/mediaLegs.png')] public static var legs_img:Class;
		[Embed(source = '../../assets/mediaArm.png')] public static var arms_img:Class;
		
		[Embed(source = "../../assets/som/oh.mp3")]
		public static var attentionSound:Class;
		
		[Embed(source = "../../assets/som/paparazzi.mp3")]
		public static var attentionSound2:Class;
		
		[Embed(source = "../../assets/photo.png")]
		public static var photoImg:Class;
		public var photoSprite:FlxSprite;
		
		public var stalking:Boolean = false;
		public var stalkingCount:int = 0;
		public var stalkVelocity:Number = 50;
		
		public var ativo:Boolean = true;
		
		public function Midia()
		{
			photoSprite = new FlxSprite(-100, -100, photoImg);
			photoSprite.alpha = 0.8;
			super();
		}
		
		override protected function chase():void
		{
			if (stalking) return;
			
			var dist:Number;
			if (!chasing)
			{	
				if (!hasChased)
				{
					var playerPos:FlxPoint = new FlxPoint(mainState.salazar.x, mainState.salazar.y);
					dist = Math.sqrt((playerPos.x - x) * (playerPos.x - x) + (playerPos.y - y) * (playerPos.y - y));
					
					if (dist < noticeRadius)
					{
						//FlxG.play(attentionSound);
						FlxG.play(Policial.attentionSound);
						beginChaseAnimation();
						chasing = true;
						chasePos = playerPos;
						chaseVelocity = new FlxPoint(playerPos.x - x, playerPos.y - y);
						chaseVelocity.x *= chaseSpeed / dist;
						chaseVelocity.y *= chaseSpeed / dist;
						
						keepVelocity.x = velocity.x;
						keepVelocity.y = velocity.y;
						
						velocity.x = 0;
						velocity.y = 0;
					}
				}
			}
			else
			{
				x += chaseVelocity.x;
				y += chaseVelocity.y;
				
				dist = Math.sqrt((chasePos.x - x) * (chasePos.x - x) + (chasePos.y - y) * (chasePos.y - y));
				if (dist < chaseMinDist)
				{
					chasing = false;
					hasChased = true;
					
					velocity.x = keepVelocity.x;
					velocity.y = keepVelocity.y;
				}
			}
		}
		
		public function respawn(state:MainState, pos:FlxPoint, dir:int):void
		{
			init(state, pos, dir, head_img, body_img, legs_img, arms_img);
			
			mainState = state;
			
			hasChased = false;
			chasing = false;
		}
		
		override public function update():void 
		{
			photoSprite.x = this.x - 10 + 10 * scale.x;
			photoSprite.y = this.y - 30;
			if (stalking) 
			{
				stalk()
			}
			super.update();
			
		}
		
		override public function draw():void
		{
			super.draw();
			if (stalkingCount / 3 % 3 == 0 && stalking)
			{
				photoSprite.draw();
			}
		}
		
		private function stalk():void
		{
			stalkingCount++;
			
			scaleY = 1 + 0.1*Math.sin(stalkingCount/3);
			
			var sal:Salazar = mainState.salazar;
			
			var dx:Number = sal.x - x;
			var dy:Number = sal.y - y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			
			if (dist > sal.individualityBubble)
			{
				var novaVel:Point = new Point(dx, dy);
				novaVel.normalize(stalkVelocity);
				velocity.x = novaVel.x;
				velocity.y = novaVel.y;
			}
		}
		
		public function beginStalk():void
		{
			if (stalking) return;
			if(!stalking) FlxG.play(attentionSound2);
			stalking = true;
		}
		
		public override function destroy():void
		{
			if (stalking) state.salazar.slowed = false;
			super.destroy();
		}
		
		public override function kill():void
		{
			if (stalking) state.salazar.slowed = false;
			super.kill();
		}
		
		public function endStalk():void
		{
			stalking = false;
			
			var dir:int = Math.random() > 0.5 ? 1 : -1;
			
			velocity.x = speed_x * dir;
			velocity.y = speed_y * dir;
			setScale(dir, scale.y);
			
			solid = true;
			
			if (Math.random() < 0.5)
			{
				velocity.y *= -1;
			}
			
		}
	}
}